package com.bjsxt.controller;

import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService  adminService;

    //添加管理员实现
    @RequestMapping("/adminSave")
    @ResponseBody
    public    Integer   adminSave(Admin admin,Integer rid){

        return   adminService.save(admin,rid);
    }


     //查询所有用户实现
    @RequestMapping("/adminFindAll")
    @ResponseBody
     public PageA<Admin>  adminFindAll(Integer page,Integer rows){

         return   adminService.findAll(page, rows);
     }

    //用户退出实现
    @RequestMapping("/adminLoginOut")
    public  String     adminLoginOut(HttpSession session){

        //手动注销session
        session.invalidate();
        //跳转到登录页面
        return "redirect:/login.jsp";
    }


    //用户登录实现
    @ResponseBody
    @RequestMapping("/adminLogin")
    public   boolean  adminLogin(String  aname, String  apwd, HttpSession  session){

        Admin admin = adminService.login(aname, apwd);

        if(admin!=null){
            session.setAttribute("admin",admin);
            return   true;
        }
        return  false;

    }


}
